package com.adfm.bo.impl;

import com.adfm.bo.Devise;
import com.adfm.bo.EtatTransaction;
import com.adfm.bo.IPaiement;
import com.adfm.bo.JetonRetour;

public class PaiementImpl implements IPaiement {

	@Override
	public JetonRetour ouvertureTransaction(double somme, Devise devise,
			String urlRetour, Long timeOut) {
		JetonRetour jr = new JetonRetour();
		jr.setUuidRequete("TRAN-" + (System.currentTimeMillis() % 100000));
		jr.setUrlRedirection("http://maBanque/paiement");

		String prefix = "SP [" + jr.getUuidRequete() + "] ";
		System.out.println(prefix + "ouvertureTransaction");

		return jr;
	}

	@Override
	public EtatTransaction pollingEtat(String uuidTransaction) {
		long n = System.currentTimeMillis() % 100;

		EtatTransaction state;
		if (n < 10) {
			state = EtatTransaction.DONE;
		} else if (n < 15) {
			state = EtatTransaction.OUTOFTIME;
		} else if (n < 20) {
			state = EtatTransaction.REFUSE;
		} else {
			state = EtatTransaction.ENCOURS;
		}
		String prefix = "SP [" + uuidTransaction + "] ";
		System.out.println(prefix + "polling - transaction en ... " + state) ;

		return state;
	}

}